Text File Operations
A file is a collection of information, usually stored on a computer's disk. Information can be saved to files and then later reused.
The programs you have written so far require you to re-enter data each time the program runs. This is because the data stored in RAM disappears once the program stops running or the computer is shut down. If a program is to retain data between the times it runs, it must have a way of saving it. Information is saved in a file and will remain there after he program stops running. The information can then be retrieved and used at a later time.
The type of file we will be working with is called a text file. It is a file in which the data is readable and in the same form as it would be entered from a keyboard. Non-text files are usually referred to as binary, which is a file that contains formatting characters and other non-readable data. A binary file is normally not easily readable or not readable without using the software that created it.
Text files are also sequential, which means you can only access data by starting at the beginning of the file and working through it one piece of data at a time.
In order to use text files, your program will need the following steps or commands:
1. #include <fstream>
This is the header file that handles file input and output.
2. Declare one or more file variables.
There are three file types: ifstream, ofstream, and fstream. ifstream is used for files used for input; ifstream variables can be used to read but not write, ofstream is used for files used for output; ofstream variables can be used write but not read, fstream is used for a file for which you don't want to specify its use at this point in the program. Sample declarations are as follows:
ifstream inFile; // creates a file variable called inFile that can only be used for input
ofstream outFile; // creates a file variable called outFile that can only be used for output
fstream someFile; //
creates a file variable called someFile but does not
specify how it will be used
3. Open the file.
A file has to be opened before you can use it. In the open statement, you specify how the file is to be used (if this wasn't done when it was declared) and specify the file's name as used by DOS or Windows. Using the variables shown above, the files could be opened as follows:
inFile.open("a:\\payroll.dat"); // opens inFile for input and associates it with the file's "real" name outFile.open("a:\\payroll.out"); // opens outFile for output and associates it with the file's "real" name someFile.open("a:\\payroll.dat", ios::in); // opens someFile for input; for output, use ios::out
4. Read the file. (inFile can now be used exactly as you would use cin)
InFile » name
» payRate » hours;
InFile.getline(stringName, 80);
5. Write to the file. (outFile can now be used exactly as you would use cout)
outfile « setw(10) « setprecision(2)«
hours;
outFile. width(lO);
outFile.precision(
1);
outFile« setiosflags(ios::showpoint | ios::fixed);
outFile « name « '\t' « payRate « endl;
6. Close the file.
When you close the file, you are
freeing it for other users, and forcing the operating system to complete any
writing to disk. If you want to repeat reading any items in the file, you need
to close it and reopen it to reposition the file pointer at the beginning and
start working your way through the file again. The statements to close the file
are as follows:
inFile.close();
outFile.close();
Notes by Chuck Young, web page by Jim Engel